/*
 * buffhandle.c
 *
 *  Created on: 2009-5-22
 *      Author: hwg
 */
#include "../header/common.h"

int initbuff( buff *bu, char *header)
{
	bu->buff = header;
	bu->current = 0;
	bu->count = 1;
	bu->size = strlen(header);
	return ok;
}

int getkeynode(buff *bu , keynode *node)
{
	if(bu->current < bu->size - 1)
	{
		int i = bu->current;
		int j = 0;
		while(((bu->buff[i] == '\n')||(bu->buff[i] == ' ')||(bu->buff[i] == '\t'))&& (i< (bu->size))) i=i+1;
		while((bu->buff[i] != ';')&& (i< (bu->size)))
		{
			node->words[j] = bu->buff[i];
			i = i+1;
			j= j+1;
		}
		i = i+1;
		node->words[j] = ';';
		node->words[j+1] = '\0';
		while(((bu->buff[i] == '\n')||(bu->buff[i] == ' ')||(bu->buff[i] == '\t'))&& (i< (bu->size))) i=i+1;
		j = 0;
		while((bu->buff[i] != '#')&& (i< bu->size))
		{
			node->exp[j] = bu->buff[i];
			i = i+1;
			j= j+1;
		}
		i = i + 1;
		node->exp[j] = '\0';

		bu->current = i;
		node->next = NULL;
		return ok;
	}
	else
	{
		return error;
	}

}
int getwordsnode(buff *bu , wordnode *node)
{
	if(bu->current < bu->size - 1)
	{
		int i = bu->current;
		int j = 0;
		while(((bu->buff[i] == '\n')||(bu->buff[i] == ' ')||(bu->buff[i] == '\t'))&& (i< (bu->size))) i=i+1;
		while((bu->buff[i] != '\n')&&(bu->buff[i] != ' ')&&(bu->buff[i] != '\t')&& (i< (bu->size)))
		{
			node->words[j] = bu->buff[i];
			i = i+1;
			j= j+1;
		}
		i = i+1;
		node->words[j] = ';';
		node->words[j+1] = '\0';
		node->order = bu->count;
		bu->count = bu->count + 1;
		bu->current = i;
		node->next = NULL;
		return ok;
	}
	else
	{
		return error;
	}
}

int getactionnode(buff *bu ,int *ident, elmAction *node)
{
	if(bu->current < bu->size - 1)
	{
		int temp;
		int i = bu->current;
		while(((bu->buff[i] != '('))&& (i< (bu->size))) i=i+1;
		i = i + 1;
		*ident = (int)(bu->buff[i]) - 48; i = i + 1;
		while(((bu->buff[i] != ','))&& (i< (bu->size)))
		{
			temp = (int)(bu->buff[i]) - 48;
			*ident = (*ident) * 10 + temp;
			i=i+1;
		}
		i = i + 1;
		node->identyfy = bu->buff[i]; i = i + 1;
		while(((bu->buff[i] != ','))&& (i< (bu->size))) i=i+1;
		i = i + 1;
		node->action = bu->buff[i]; i = i + 1;
		while(((bu->buff[i] != ','))&& (i< (bu->size))) i=i+1;
		i = i + 1;
		node->number = (int)(bu->buff[i]) - 48; i = i + 1;
		temp = 0;
		while(((bu->buff[i] != ','))&& (i< (bu->size)))
		{
			temp = (int)(bu->buff[i]) - 48;
			node->number = (node->number) * 10 + temp;
			i=i+1;
		}
		i = i + 1;
		temp = 0;
		node->go = (int)(bu->buff[i]) - 48; i = i + 1;
		while(((bu->buff[i] != ')'))&& (i< (bu->size)))
		{
			temp = (int)(bu->buff[i]) - 48;
			node->go = (node->go) * 10 + temp;
			i=i+1;
		}
		i = i + 1;
		bu->current = i;
		return ok;
	}
	else
	{
		return error;
	}
}
int getgramexp(buff *bu , productnode *node)
{
	if(bu->current < bu->size - 1)
	{
		int i = bu->current;
		int j = 0;
		getothercodeindex(bu , &i);
		node->index = (int)(bu->buff[i]) - 48; i = i + 1;
		getothercodeindex(bu , &i);
		node->h = bu->buff[i]; i = i + 1;
		getothercodeindex(bu , &i);
		while(isavliablebuff(bu , i) == ok)
		{
			node->express[j]= bu->buff[i];
			i = i+1;
			j= j+1;
		}
		i = i+1;
		node->express[j] = '\0';
		j = 0;
		getothercodeindex(bu , &i);
		while(isavliablebuff(bu , i) == ok)
		{
			putchar(bu->buff[i]);
			node->middlecode[j]= bu->buff[i];
			i = i+1;
			j= j+1;
		}
		i = i+1;
		node->middlecode[j] = '\0';
		bu->current = i;
		return ok;
	}
	else
	{
		return error;
	}
}
void getothercodeindex(buff *bu , int *i)
{
	while(((bu->buff[*i] == '\n')||(bu->buff[*i] == ' ')||(bu->buff[*i] == '\t')||(bu->buff[*i] == '\v')||(bu->buff[*i] == '\r')||(bu->buff[*i] == '\f')||(bu->buff[*i] == '>'))&& (*i< (bu->size))) *i=*i+1;
}
int isavliablebuff(buff *bu , int i)
{
	if ((bu->buff[i] != '\n')&&(bu->buff[i] != ' ')&&(bu->buff[i] != '\t')&& (i< (bu->size)))
		return ok;
	else
		return error;
}










